<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row style="margin-bottom: 20px">
          <a-col :span="20">
            <a-row>
              <a-col :span="20" :offset="2" style="color:orange">
                {{$t('说明：')}}
              </a-col>
              <a-col :span="20" :offset="2" style="color:orange">
                {{$t('1、系统默认的短信账户仅支持发送安全认证时的短信验证码。')}}
              </a-col>
              <a-col :span="20" :offset="2" style="color:orange">
                {{$t('2、若需要给会员批量发送活动短信，请联系短信平台‘程经理[电话 18611733312]或[微信号 Cgm_jiayou]’开通自己的短信账户')}}
              </a-col>
            </a-row>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="10">
            <a-form-model-item
              :label="$t('SMS类型')"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="smsType"
            >
              <j-dict-select-tag type="list" v-model="model.smsType" dictCode="sms_type" :placeholder="$t('请选择')" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row v-if="model.smsType === '0'">
          <a-col :span="10">
            <a-form-model-item
              :label="$t('账户')"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="accountId"
            >
              <a-input v-model="model.accountId" :placeholder="$t('请输入账户')"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row v-if="model.smsType === '0'">
          <a-col :span="10">
            <a-form-model-item
              :label="$t('密码')"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="accountPwd"
            >
              <a-input v-model="model.accountPwd" :placeholder="$t('请输入密码')"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row v-if="model.smsType === '0'">
          <a-col :span="10">
            <a-form-model-item
              :label="$t('签名')"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="sign"
            >
              <a-input :disabled="model.accountId=='400637'" v-model="model.sign" :placeholder="$t('请输入签名')"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row v-if="model.smsType === '1'">
          <a-col :span="10">
            <a-form-model-item
              :label="$t('阿里AccessKeyID')"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="smsAccessKeyId"
            >
              <a-input v-model="model.smsAccessKeyId" :placeholder="$t('请输入阿里AccessKeyID')"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row v-if="model.smsType === '1'">
          <a-col :span="10">
            <a-form-model-item
              :label="$t('阿里AccessKeySecret')"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="smsAccessKeySecret"
            >
              <a-input v-model="model.smsAccessKeySecret" :placeholder="$t('请输入阿里AccessKeySecret')"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row v-if="model.smsType === '1'">
          <a-col :span="10">
            <a-form-model-item
              :label="$t('签名')"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="smsSignName"
            >
              <a-input v-model="model.smsSignName" :placeholder="$t('请输入签名')"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row v-if="model.smsType === '1'">
          <a-col :span="10">
            <a-form-model-item
              :label="$t('模板CODE')"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="smsTemplateCode"
            >
              <a-input v-model="model.smsTemplateCode" :placeholder="$t('请输入模板CODE')"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="10">
            <a-row>
              <a-col :span="16" :offset="8">
                <a-button type="primary" :disabled="formDisabled" @click="submitForm" v-has="'BisSysParameterController:edit'">{{$t('保存')}}</a-button>
                <!-- <a @click="test">短信测试</a-button> -->
              </a-col>
            </a-row>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction } from '@/api/manage'

export default {
  name: 'SMSserviceForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        smsType: '0'
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        smsType: [{required: true, message: this.$t('请选择') }],
        accountId: [{ required: true, message: this.$t('请输入账户') }],
        accountPwd: [{ required: true, message: this.$t('请输入密码') }],
        sign: [{ required: true, message: this.$t('请输入签名') }],
        smsAccessKeyId: [{ required: true, message: this.$t('请输入阿里AccessKeyID') }],
        smsAccessKeySecret: [{ required: true, message: this.$t('请输入阿里AccessKeySecret') }],
        smsSignName: [{ required: true, message: this.$t('请输入签名') }],
        smsTemplateCode: [{ required: true, message: this.$t('请输入模板CODE') }],
      },
      url: {
        add: '/bis/bisSysParameter/saveBatch',
        edit: '/bis/bisSysParameter/saveBatch',
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      console.log(record)
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      let that = this
      // 触发表单验证
      that.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = '/bis/bisSysParameter/saveBatch'
          let method = 'post'
          let query=[]
          query.push({
            id:that.model.idKey?that.model.idKey:'',
            name:'shumiAccountId',
            svalue:that.model.accountId,
            comment: '数米账户账号'
          })
          query.push({
            id:that.model.pwdKey?that.model.pwdKey:'',
            name:'shumiAccountPwd',
            svalue:that.model.accountPwd,
            comment: '数米账户密码'
          })
          query.push({
            id:that.model.sKey?that.model.sKey:'',
            name:'shumiSign',
            svalue:that.model.accountId=='400637'?'新盈加':that.model.sign,
            comment: '数米账户签名'
          })
          query.push({
            id:that.model.stKey?that.model.stKey:'',
            name:'smsType',
            ivalue:that.model.smsType,
            comment: 'SMS类型'
          })
          query.push({
            id:that.model.siKey?that.model.siKey:'',
            name:'smsAccessKeyId',
            svalue:that.model.smsAccessKeyId,
            comment: 'SMS阿里AccessKeyID'
          })
          query.push({
            id:that.model.ssKey?that.model.ssKey:'',
            name:'smsAccessKeySecret',
            svalue:that.model.smsAccessKeySecret,
            comment: 'SMS阿里AccessKeySecret'
          })
          query.push({
            id:that.model.snKey?that.model.snKey:'',
            name:'smsSignName',
            svalue:that.model.smsSignName,
            comment: 'SMS阿里签名'
          })
          query.push({
            id:that.model.scKey?that.model.scKey:'',
            name:'smsTemplateCode',
            svalue:that.model.smsTemplateCode,
            comment: 'SMS阿里模版CODE'
          })
          httpAction(httpurl, query, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    }
  }
}
</script>